package com.shuangyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shuangyu.common.R;
import com.shuangyu.entity.ShoppingCart;
import com.shuangyu.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 首页显示
     * @param request
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpServletRequest request){

        Long userId = (long) request.getSession().getAttribute("user");

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }

    /**
     * 添加菜品进购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart){
        ShoppingCart one = shoppingCartService.getOneById(shoppingCart);

        if (one != null){
            LambdaUpdateWrapper<ShoppingCart> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(ShoppingCart::getNumber,one.getNumber()+1)
                    .eq(ShoppingCart::getUserId, shoppingCart.getUserId())
                    .eq(ShoppingCart::getId, one.getId());
            shoppingCartService.update(updateWrapper);
            return R.success("操作成功");
        }

        shoppingCartService.save(shoppingCart);

        return R.success("操作成功");
    }

    /**
     * 减少购物车
     * @param shoppingCart
     * @return R
     */
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){

        ShoppingCart one = shoppingCartService.getOneById(shoppingCart);

        if (one.getNumber() > 1){
            LambdaUpdateWrapper<ShoppingCart> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(ShoppingCart::getNumber,one.getNumber()-1)
                    .eq(ShoppingCart::getUserId, shoppingCart.getUserId())
                    .eq(ShoppingCart::getId, one.getId());
            shoppingCartService.update(updateWrapper);
            return R.success("操作成功");
        }

        if (one.getNumber() == 1){
            shoppingCartService.removeById(one);
            return R.success("操作成功");
        }

        return R.success("操作成功");
    }

    /**
     * 清空购物车
     * @param request
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(HttpServletRequest request){

        Long userId = (long) request.getSession().getAttribute("user");

        HashMap<String, Object> map = new HashMap<>();
        map.put("user_id", userId);
        shoppingCartService.removeByMap(map);

        return R.success("操作成功");
    }



}
